import cv2
import numpy as np
img_path='traffic.png'
img=cv2.imread(img_path)
cv2.imshow("Input",img)
imgContour=img.copy()
imgGray=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
imgBlur=cv2.GaussianBlur(imgGray,(5,5),1)
cv2.imshow("Blur",imgBlur)

imgCanny=cv2.Canny(imgBlur,70,200)
cv2.imshow("Canny",imgCanny)
contours,hierarchy=cv2.findContours(imgCanny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
imgContour=cv2.drawContours(imgContour,contours,-1,(0,255,255),2)
cv2.imshow("Contours",imgContour)

for obj in contours:
    perimeter=cv2.arcLength(obj,True)
    approx=cv2.approxPolyDP(obj,0.02*perimeter,True)
    CornerNum=len(approx)
    x,y,w,h=cv2.boundingRect(approx)
    if CornerNum==3:
        objType="WarningSign"
    elif CornerNum==4:
        area=cv2.contourArea(obj)
        circularity=4*np.pi*area/(perimeter**2)
        if circularity>0.8:
            objType="SpeedLimit"
        cv2.rectangle(imgContour,(x,y),(x+w,y+h),(0,0,255),1)
        cv2.putText(imgContour,objType,(x,y+h+20),cv2.FONT_HERSHEY_COMPLEX,0.6,(0,0,0),1)
    cv2.imshow("Identification",imgContour)
    cv2.waitKey()
    cv2.destroyAllWindows()